import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-10-09
 * Time: 20:22
 */
public class Sort {
    // 选择排序
    public static void selectSort(int[] array){
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            Swap(array, left, minIndex);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            Swap(array,right, maxIndex);
            left++;
            right--;
        }
    }

    private static void Swap(int[] array, int left, int right) {
        int tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
    }


    // 堆排序
    public static void heapSort(int[] array){
        //第一步：创建一个大根堆：
        createHeap(array);
        int end = array.length - 1;
        //第二步：拿最后一个元素与栈顶元素进行交换，循环
        while (end > 0) {
            Swap(array, 0, end);
            //进行调整
            siftDown(array, 0, end);
            end--;
        }
    }
    public  static void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent > 0; parent--) {
            siftDown(array, parent, array.length);
        }
    }
    //向下调整
    private static void siftDown(int[] array, int parent, int length) {
        int child = parent * 2 + 1;
        while (child < length) {
            //找到子树中较大的节点值
            if (child + 1 < length && array[child] < array[child + 1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                Swap(array, parent, child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flag = false;
            for (int j = i; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    Swap(array, j, j + 1);
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    // 快速
    public static void quickSort(int[] array) {
        //quick(array, 0, array.length - 1); //没有优化的快速排序
        //quickOp(array, 0, array.length - 1); //优化过的快速排序
        quickNor(array, 0, array.length - 1); //非递归的快速排序
    }

    private static void quickNor(int[] array, int start, int end) {
        //第一步，创建一个栈
        Deque<Integer> stack = new ArrayDeque<>();
        int pivot = partition(array, start, end);
        //从左边开始放：
        if (pivot > start + 1) {
            stack.push(start);
            stack.push(pivot - 1);
        }
        if (pivot < end - 1) {
            stack.push(pivot + 1);
            stack.push(end);
        }
        //栈都是从栈顶元素开始弹出
        while(!stack.isEmpty()) {
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array, start, end);
            //从左边开始放：
            if (pivot > start + 1) {
                stack.push(start);
                stack.push(pivot - 1);
            }
            if (pivot < end - 1) {
                stack.push(pivot + 1);
                stack.push(end);
            }
        }
    }

    private static void quickOp(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        //2. 递归到小的子区间时，可以考虑使用插入排序
        if (end - start + 1 <= 10) {
            insertSortRange(array, start, end);
        }
        //1. 三数取中法选key,使得二叉树的分布更加的均衡
        int minIndex = getMiddleNum(array, start, end);
        Swap(array, start, minIndex);
        int pivot = partition(array, start, end);
        quick(array, start, pivot);
        quick(array, pivot + 1, end);
    }
    //直接插入排序
    private static void insertSortRange(int[] array, int start, int end) {
        for (int i = start + 1; i <= end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j > 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    array[j + 1] = tmp;
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private static int getMiddleNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (array[mid] > array[left]) {
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int pivot = partition(array, start, end);
        quick(array, start, pivot);
        quick(array, pivot + 1, end);
    }

    //挖坑法
    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
}
